<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增在线实验项目')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-experiment-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">实验名称：</label>
                <div class="col-sm-8">
                    <input name="experimentName" id="experimentName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">实验链接：</label>
                <div class="col-sm-8">
                    <input name="experimentHref" id="experimentHref" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">实验说明：</label>
                <div class="col-sm-8">
                    <!--<input name="experimentExplain" class="form-control" type="text">-->
                    <div class="form-group">
                        <div id="summernote1">

                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group" hidden="hidden">
                <label class="col-sm-3 control-label">实验报告：</label>
                <div class="col-sm-8">
                    <div class="form-group">
                        <div id="summernote2">
                            <div style="width: 100%">
                                <table class="table">
                                    <tr><td width="100/6%">实验名称</td><td width="100/6%"></td><td width="100/6%">专业/班级</td><td width="100/6%"></td><td width="100/6%">实验日期</td><td width="100/6%"></td></tr>
                                    <tr><td>姓名</td><td></td><td>学号</td><td></td><td>指导教师</td><td></td></tr>
                                    <tr><td>实验成绩</td><td colspan="5"></td></tr>
                                    <tr><td colspan="6" style='height: 100px'><p>一：实验目的</p></td></tr>
                                    <tr><td colspan="6" style='height: 100px'><p>二：实验步骤</p></td></tr>
                                    <tr><td colspan="6" style='height: 100px'><p>三：实验心得(实验收获：发现的问题及解决情况；改进建议)</p></td></tr>
                                    <tr><td colspan="6" style='height: 100px'><p>四：指导教师评价</p></td></tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">封面上传：</label>
                <!--前端如何显示回显图片？？-->
                <div class="col-sm-7">
                    <div class="form-group">
                        <div class="file-loading">
                            <input id="file" type="file" multiple name="file" required>
                            <!--<input id="onlineExperimentImg" hidden="hidden"/>-->
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">是否开放：</label>
                <div class="col-sm-8">
                    <!--<input name="ifOpen" class="form-control" type="text">-->
                    <select name="ifOpen" class="form-control" required id="ifOpen">
                        <option value="1">开放</option>
                        <option value="2">不开放</option>
                    </select>
                </div>
            </div>
            <div class="form-group" style="text-align: center">
                <div class="col-sm-5 control-label">
                    <button class="btn btn-lg btn-success" type="button" onclick="submitHandler()">保存</button>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "system/experiment"
        $("#form-experiment-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            var formData = new FormData();
            formData.append("experimentName",$("#experimentName").val())
            formData.append("experimentHref",$("#experimentHref").val())
            formData.append("experimentExplain",$("#summernote1").summernote("code"))
            formData.append("experimentReport",$("#summernote2").summernote("code"))
            formData.append("file",$("#file")[0].files[0])
            formData.append("ifOpen",$("#ifOpen").val())
            if ($.validate.form()) {
                $.ajax({
                    url: prefix + "/add",
                    type: "POST",
                    data : formData,
                    cache: false,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        console.log(data)
                        if(data.code == 0){
                            $.modal.msgReload(data.msg,'success')
                        }else{
                            $.modal.msgReload(data.msg,'error')
                        }
                    }
                })
                //$.operate.save(prefix + "/add", $('#form-experiment-add').serialize());
            }
        }

        //格式化文件上传
        $('#file').fileinput({
            allowedFileExtensions: ['jpg', 'png', 'gif'],//接收的文件后缀,
            enctype: 'multipart/form-data',
            showBrowse: true,
            showUpload: false, //是否显示上传按钮
            showCaption: false,//是否显示标题
            browseClass: "btn btn-success", //按钮样式
            previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
            initialPreviewAsData: true,
            initialPreviewFileType: 'image',
            //initialPreview: ctx + $("#onlineExperimentImg").val(), //要显示的图片的路径
            //msgFilesTooMany: "选择上传的文件数量({2}) 超过允许的最大数值{2}！",
        });


        $('#summernote1').summernote({
            placeholder: '请输入实验报告模板',
            height: 500,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function(files, editor, $editable) {
                    var formData = new FormData();
                    formData.append("file", files[0]);
                    $.ajax({
                        type: "POST",
                        url: ctx + "common/upload",
                        data: formData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function(result) {
                            if (result.code == web_status.SUCCESS) {
                                //alert(result.url+","+result.fileName)
                                $('.summernote2').summernote('editor.insertImage', result.url, result.fileName);
                            } else {
                                $.modal.alertError(result.msg);
                            }
                        },
                        error: function(error) {
                            $.modal.alertWarning("图片上传失败。");
                        }
                    });
                }
            }
        });


        $('#summernote2').summernote({
            placeholder: '请输入实验报告模板',
            height: 500,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function(files, editor, $editable) {
                    var formData = new FormData();
                    formData.append("file", files[0]);
                    $.ajax({
                        type: "POST",
                        url: ctx + "common/upload",
                        data: formData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function(result) {
                            if (result.code == web_status.SUCCESS) {
                                //alert(result.url+","+result.fileName)
                                $('.summernote2').summernote('editor.insertImage', result.url, result.fileName);
                            } else {
                                $.modal.alertError(result.msg);
                            }
                        },
                        error: function(error) {
                            $.modal.alertWarning("图片上传失败。");
                        }
                    });
                }
            }
        });
    </script>
</body>
</html>